{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "68d54cda-3b75-409e-bab2-708354d71ffb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T03:40:54.389239Z",
     "iopub.status.busy": "2022-05-27T03:40:54.388786Z",
     "iopub.status.idle": "2022-05-27T03:40:54.763121Z",
     "shell.execute_reply": "2022-05-27T03:40:54.762176Z",
     "shell.execute_reply.started": "2022-05-27T03:40:54.389140Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d627234-34be-4bb1-90e1-ab5fe2deb727",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T12:11:11.061463Z",
     "iopub.status.busy": "2022-05-27T12:11:11.061059Z",
     "iopub.status.idle": "2022-05-27T12:11:11.065346Z",
     "shell.execute_reply": "2022-05-27T12:11:11.064635Z",
     "shell.execute_reply.started": "2022-05-27T12:11:11.061437Z"
    },
    "tags": []
   },
   "source": [
    "## 一、merge函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c687b3c-8a81-4271-bdbb-4fb3e8a1e776",
   "metadata": {},
   "source": [
    "### 1.1 同标签名的数据连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5ffad856-b59b-409a-8c93-b5e4b9d8f7ca",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T07:35:22.094701Z",
     "iopub.status.busy": "2022-05-27T07:35:22.094380Z",
     "iopub.status.idle": "2022-05-27T07:35:22.104352Z",
     "shell.execute_reply": "2022-05-27T07:35:22.103602Z",
     "shell.execute_reply.started": "2022-05-27T07:35:22.094678Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>k1</td>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>k1</td>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2   A   B\n",
       "0   k0   k0  A0  B0\n",
       "1   k0   k1  A1  B1\n",
       "2   k1   k0  A2  B2\n",
       "3   k2   k1  A3  B3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据，data1\n",
    "data1 = pd.DataFrame(\n",
    "    {\n",
    "        \"key1\": [\"k0\", \"k0\", \"k1\", \"k2\"],\n",
    "        \"key2\": [\"k0\", \"k1\", \"k0\", \"k1\"],\n",
    "        \"A\": [\"A0\", \"A1\", \"A2\", \"A3\"],\n",
    "        \"B\": [\"B0\", \"B1\", \"B2\", \"B3\"],\n",
    "    }\n",
    ")\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "333b7fe4-f7a4-437d-a69c-6c6e48839487",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T07:36:15.950129Z",
     "iopub.status.busy": "2022-05-27T07:36:15.949778Z",
     "iopub.status.idle": "2022-05-27T07:36:15.961772Z",
     "shell.execute_reply": "2022-05-27T07:36:15.960665Z",
     "shell.execute_reply.started": "2022-05-27T07:36:15.950103Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>k0</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2   C   D\n",
       "0   k0   k0  C0  D0\n",
       "1   k1   k0  C1  D1\n",
       "2   k1   k0  C2  D2\n",
       "3   k2   k0  C3  D3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据，data2\n",
    "data2 = pd.DataFrame(\n",
    "    {\n",
    "        \"key1\": [\"k0\", \"k1\", \"k1\", \"k2\"],\n",
    "        \"key2\": [\"k0\", \"k0\", \"k0\", \"k0\"],\n",
    "        \"C\": [\"C0\", \"C1\", \"C2\", \"C3\"],\n",
    "        \"D\": [\"D0\", \"D1\", \"D2\", \"D3\"],\n",
    "    }\n",
    ")\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e86ffcf1-aadb-4b4c-97fa-5e419eef8859",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T07:36:24.775526Z",
     "iopub.status.busy": "2022-05-27T07:36:24.775070Z",
     "iopub.status.idle": "2022-05-27T07:36:24.788175Z",
     "shell.execute_reply": "2022-05-27T07:36:24.787428Z",
     "shell.execute_reply.started": "2022-05-27T07:36:24.775497Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2   A   B   C   D\n",
       "0   k0   k0  A0  B0  C0  D0\n",
       "1   k1   k0  A2  B2  C1  D1\n",
       "2   k1   k0  A2  B2  C2  D2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据连接，默认-内连接\n",
    "pd.merge(data1, data2)  # pd.merge(data1, data2, how=\"inner\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9c05bf7d-86b3-42df-994b-877eceb88091",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T07:59:13.317878Z",
     "iopub.status.busy": "2022-05-27T07:59:13.317516Z",
     "iopub.status.idle": "2022-05-27T07:59:13.333675Z",
     "shell.execute_reply": "2022-05-27T07:59:13.332462Z",
     "shell.execute_reply.started": "2022-05-27T07:59:13.317852Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>k1</td>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>k2</td>\n",
       "      <td>k1</td>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>k2</td>\n",
       "      <td>k0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2    A    B    C    D\n",
       "0   k0   k0   A0   B0   C0   D0\n",
       "1   k0   k1   A1   B1  NaN  NaN\n",
       "2   k1   k0   A2   B2   C1   D1\n",
       "3   k1   k0   A2   B2   C2   D2\n",
       "4   k2   k1   A3   B3  NaN  NaN\n",
       "5   k2   k0  NaN  NaN   C3   D3"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据连接，外连接（全连接）\n",
    "pd.merge(data1, data2, how=\"outer\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "250f3bbd-8b00-48f7-b44b-1dd2192053c4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T07:47:57.252911Z",
     "iopub.status.busy": "2022-05-27T07:47:57.252568Z",
     "iopub.status.idle": "2022-05-27T07:47:57.266976Z",
     "shell.execute_reply": "2022-05-27T07:47:57.265542Z",
     "shell.execute_reply.started": "2022-05-27T07:47:57.252885Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>k1</td>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>k2</td>\n",
       "      <td>k1</td>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2   A   B    C    D\n",
       "0   k0   k0  A0  B0   C0   D0\n",
       "1   k0   k1  A1  B1  NaN  NaN\n",
       "2   k1   k0  A2  B2   C1   D1\n",
       "3   k1   k0  A2  B2   C2   D2\n",
       "4   k2   k1  A3  B3  NaN  NaN"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据连接，左连接\n",
    "pd.merge(data1, data2, how=\"left\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cdb86329-4ec0-491d-a023-34b78da4e27d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T07:48:17.382740Z",
     "iopub.status.busy": "2022-05-27T07:48:17.382403Z",
     "iopub.status.idle": "2022-05-27T07:48:17.397429Z",
     "shell.execute_reply": "2022-05-27T07:48:17.396725Z",
     "shell.execute_reply.started": "2022-05-27T07:48:17.382715Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>k0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2    A    B   C   D\n",
       "0   k0   k0   A0   B0  C0  D0\n",
       "1   k1   k0   A2   B2  C1  D1\n",
       "2   k1   k0   A2   B2  C2  D2\n",
       "3   k2   k0  NaN  NaN  C3  D3"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据连接，右连接\n",
    "pd.merge(data1, data2, how=\"right\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "70d8eef7-4619-40e7-8928-9d673dc7efe5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-28T08:53:00.663933Z",
     "iopub.status.busy": "2022-05-28T08:53:00.663301Z",
     "iopub.status.idle": "2022-05-28T08:53:00.713457Z",
     "shell.execute_reply": "2022-05-28T08:53:00.712599Z",
     "shell.execute_reply.started": "2022-05-28T08:53:00.663866Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2_x</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>key2_y</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>k0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>k1</td>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>k0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>k0</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>k0</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>k2</td>\n",
       "      <td>k1</td>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>k0</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2_x   A   B key2_y   C   D\n",
       "0   k0     k0  A0  B0     k0  C0  D0\n",
       "1   k0     k1  A1  B1     k0  C0  D0\n",
       "2   k1     k0  A2  B2     k0  C1  D1\n",
       "3   k1     k0  A2  B2     k0  C2  D2\n",
       "4   k2     k1  A3  B3     k0  C3  D3"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据连接，指定连接字段\n",
    "pd.merge(data1, data2, on=\"key1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f5d96498-166a-4952-8887-9317b0f200c3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-28T09:09:35.033610Z",
     "iopub.status.busy": "2022-05-28T09:09:35.032933Z",
     "iopub.status.idle": "2022-05-28T09:09:35.044967Z",
     "shell.execute_reply": "2022-05-28T09:09:35.043991Z",
     "shell.execute_reply.started": "2022-05-28T09:09:35.033585Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2   A   B   C   D\n",
       "0   k0   k0  A0  B0  C0  D0\n",
       "1   k1   k0  A2  B2  C1  D1\n",
       "2   k1   k0  A2  B2  C2  D2"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据连接，指定连接字段\n",
    "pd.merge(data1, data2, on=[\"key1\", \"key2\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "176b3f25-b0ac-42b8-a5b5-d45a2df3ce18",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-28T09:19:07.864343Z",
     "iopub.status.busy": "2022-05-28T09:19:07.863962Z",
     "iopub.status.idle": "2022-05-28T09:19:07.878040Z",
     "shell.execute_reply": "2022-05-28T09:19:07.876230Z",
     "shell.execute_reply.started": "2022-05-28T09:19:07.864297Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>key2_left</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>key2_right</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>k0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>k1</td>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>k0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>k0</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k1</td>\n",
       "      <td>k0</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>k0</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>k2</td>\n",
       "      <td>k1</td>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>k0</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1 key2_left   A   B key2_right   C   D\n",
       "0   k0        k0  A0  B0         k0  C0  D0\n",
       "1   k0        k1  A1  B1         k0  C0  D0\n",
       "2   k1        k0  A2  B2         k0  C1  D1\n",
       "3   k1        k0  A2  B2         k0  C2  D2\n",
       "4   k2        k1  A3  B3         k0  C3  D3"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 处理重复列名，加后缀\n",
    "pd.merge(data1, data2, on=\"key1\", suffixes=(\"_left\", \"_right\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6eeb8e62-256e-4f73-a589-0acfa1cf7564",
   "metadata": {},
   "source": [
    "### 1.2 不同标签名的数据连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9a3e921b-b126-4440-b9ec-210863045fc2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-28T09:35:18.208450Z",
     "iopub.status.busy": "2022-05-28T09:35:18.208027Z",
     "iopub.status.idle": "2022-05-28T09:35:18.217447Z",
     "shell.execute_reply": "2022-05-28T09:35:18.216304Z",
     "shell.execute_reply.started": "2022-05-28T09:35:18.208424Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>A1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>A2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>A3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1   A\n",
       "0   k0  A0\n",
       "1   k0  A1\n",
       "2   k1  A2\n",
       "3   k2  A3"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据，data3\n",
    "data3 = pd.DataFrame({\"key1\": [\"k0\", \"k0\", \"k1\", \"k2\"], \"A\": [\"A0\", \"A1\", \"A2\", \"A3\"]})\n",
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "63eb830e-699c-4032-bdfd-cd589a64a9bf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-28T09:56:09.879244Z",
     "iopub.status.busy": "2022-05-28T09:56:09.878672Z",
     "iopub.status.idle": "2022-05-28T09:56:09.888368Z",
     "shell.execute_reply": "2022-05-28T09:56:09.887068Z",
     "shell.execute_reply.started": "2022-05-28T09:56:09.879219Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A0</th>\n",
       "      <td>A0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A1</th>\n",
       "      <td>A1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A3</th>\n",
       "      <td>A2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     B  C\n",
       "A0  A0  0\n",
       "A1  A1  1\n",
       "A3  A2  2"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据，data4\n",
    "data4 = pd.DataFrame(\n",
    "    {\"B\": [\"A0\", \"A1\", \"A2\"], \"C\": np.arange(3)}, index=[\"A0\", \"A1\", \"A3\"]\n",
    ")\n",
    "data4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "6ac18022-c233-4faa-a2b6-42a53b16dcff",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-28T09:58:06.008664Z",
     "iopub.status.busy": "2022-05-28T09:58:06.008301Z",
     "iopub.status.idle": "2022-05-28T09:58:06.020612Z",
     "shell.execute_reply": "2022-05-28T09:58:06.019178Z",
     "shell.execute_reply.started": "2022-05-28T09:58:06.008637Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>A0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>A1</td>\n",
       "      <td>A1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>A2</td>\n",
       "      <td>A2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1   A   B  C\n",
       "0   k0  A0  A0  0\n",
       "1   k0  A1  A1  1\n",
       "2   k1  A2  A2  2"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据不同名列做连接\n",
    "pd.merge(data3, data4, left_on=\"A\", right_on=\"B\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "144cd5d5-343f-439c-a6e6-b0b55f9bed7a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-28T09:58:08.017608Z",
     "iopub.status.busy": "2022-05-28T09:58:08.016994Z",
     "iopub.status.idle": "2022-05-28T09:58:08.028648Z",
     "shell.execute_reply": "2022-05-28T09:58:08.027731Z",
     "shell.execute_reply.started": "2022-05-28T09:58:08.017580Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>A0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>A1</td>\n",
       "      <td>A1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>A3</td>\n",
       "      <td>A2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1   A   B  C\n",
       "0   k0  A0  A0  0\n",
       "1   k0  A1  A1  1\n",
       "3   k2  A3  A2  2"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据索引标签名做连接\n",
    "pd.merge(data3, data4, left_on=\"A\", right_index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dbad21e-0ebc-4998-803a-8a7dfa8c00ac",
   "metadata": {},
   "source": [
    "## 二、join函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "00f3ce76-24f5-4269-8058-6774e047d2b4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-28T10:25:52.164190Z",
     "iopub.status.busy": "2022-05-28T10:25:52.163790Z",
     "iopub.status.idle": "2022-05-28T10:25:52.172449Z",
     "shell.execute_reply": "2022-05-28T10:25:52.171764Z",
     "shell.execute_reply.started": "2022-05-28T10:25:52.164165Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    B  C\n",
       "0  A0  0\n",
       "1  A1  1\n",
       "2  A2  2"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据，data5\n",
    "data5 = pd.DataFrame({\"B\": [\"A0\", \"A1\", \"A2\"], \"C\": np.arange(3)})\n",
    "data5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "764dce8e-2a20-46da-a802-4e467bbb8a61",
   "metadata": {},
   "source": [
    "### 2.1 默认-行索引-左连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "dffbec5b-be22-42a1-a227-9c4957338a3f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T01:10:46.284476Z",
     "iopub.status.busy": "2022-05-29T01:10:46.283859Z",
     "iopub.status.idle": "2022-05-29T01:10:46.295524Z",
     "shell.execute_reply": "2022-05-29T01:10:46.294062Z",
     "shell.execute_reply.started": "2022-05-29T01:10:46.284450Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>A1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>A2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>A3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1   A    B   C\n",
       "0   k0  A0  NaN NaN\n",
       "1   k0  A1  NaN NaN\n",
       "2   k1  A2  NaN NaN\n",
       "3   k2  A3  NaN NaN"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# join数据连接，默认\n",
    "data3.join(data4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "5060cf9c-facc-4408-ba26-4b58564caec8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T01:13:04.598499Z",
     "iopub.status.busy": "2022-05-29T01:13:04.598170Z",
     "iopub.status.idle": "2022-05-29T01:13:04.609289Z",
     "shell.execute_reply": "2022-05-29T01:13:04.608340Z",
     "shell.execute_reply.started": "2022-05-29T01:13:04.598475Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>A0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>A1</td>\n",
       "      <td>A1</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>A2</td>\n",
       "      <td>A2</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>A3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1   A    B    C\n",
       "0   k0  A0   A0  0.0\n",
       "1   k0  A1   A1  1.0\n",
       "2   k1  A2   A2  2.0\n",
       "3   k2  A3  NaN  NaN"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# join数据连接，默认\n",
    "data3.join(data5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23600054-f023-4489-b312-3aa73a053515",
   "metadata": {},
   "source": [
    "### 2.2 设定连接参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "1b7fc9e7-9180-4d72-a699-8e9414523f67",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T01:34:19.297114Z",
     "iopub.status.busy": "2022-05-29T01:34:19.296674Z",
     "iopub.status.idle": "2022-05-29T01:34:19.309600Z",
     "shell.execute_reply": "2022-05-29T01:34:19.307839Z",
     "shell.execute_reply.started": "2022-05-29T01:34:19.297087Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>A0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>A1</td>\n",
       "      <td>A1</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>A2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>A3</td>\n",
       "      <td>A2</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1   A    B    C\n",
       "0   k0  A0   A0  0.0\n",
       "1   k0  A1   A1  1.0\n",
       "2   k1  A2  NaN  NaN\n",
       "3   k2  A3   A2  2.0"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# join数据连接，设定左表连接字段\n",
    "data3.join(data4, on=\"A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "99550ccc-8011-46c6-bb47-79729875dd5e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T01:37:59.601677Z",
     "iopub.status.busy": "2022-05-29T01:37:59.601339Z",
     "iopub.status.idle": "2022-05-29T01:37:59.613665Z",
     "shell.execute_reply": "2022-05-29T01:37:59.612424Z",
     "shell.execute_reply.started": "2022-05-29T01:37:59.601651Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key1</th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>k0</td>\n",
       "      <td>A0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k0</td>\n",
       "      <td>A1</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>k1</td>\n",
       "      <td>A2</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k2</td>\n",
       "      <td>A3</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key1   A    C\n",
       "0   k0  A0  0.0\n",
       "1   k0  A1  1.0\n",
       "2   k1  A2  2.0\n",
       "3   k2  A3  NaN"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# join数据连接，设定左表连接字段，右表重命名索引\n",
    "data3.join(data5.set_index(\"B\"), on=\"A\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "762f4548-989d-47ae-8751-ae1e7af01b31",
   "metadata": {},
   "source": [
    "## 三、concat函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "6744bc9a-072f-4243-b697-bdbe40ba0bad",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T03:20:40.967099Z",
     "iopub.status.busy": "2022-05-29T03:20:40.966762Z",
     "iopub.status.idle": "2022-05-29T03:20:40.975132Z",
     "shell.execute_reply": "2022-05-29T03:20:40.974333Z",
     "shell.execute_reply.started": "2022-05-29T03:20:40.967074Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "a  0  1\n",
       "b  2  3\n",
       "c  4  5"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据，data6\n",
    "data6 = pd.DataFrame(\n",
    "    np.arange(6).reshape(3, 2), index=list(\"abc\"), columns=[\"A\", \"B\"]\n",
    ")\n",
    "data6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "ec57d0ef-b862-4b8b-b013-bccc9905fc37",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T03:20:43.038372Z",
     "iopub.status.busy": "2022-05-29T03:20:43.037821Z",
     "iopub.status.idle": "2022-05-29T03:20:43.079929Z",
     "shell.execute_reply": "2022-05-29T03:20:43.061396Z",
     "shell.execute_reply.started": "2022-05-29T03:20:43.038346Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   C  D\n",
       "a  0  1\n",
       "c  2  3"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据，data7\n",
    "data7 = pd.DataFrame(\n",
    "    np.arange(4).reshape(2, 2), index=list(\"ac\"), columns=[\"C\", \"D\"]\n",
    ")\n",
    "data7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "7b95e6e6-e520-4ab6-ab0c-79db9b3b708e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T03:21:04.798793Z",
     "iopub.status.busy": "2022-05-29T03:21:04.798439Z",
     "iopub.status.idle": "2022-05-29T03:21:04.810905Z",
     "shell.execute_reply": "2022-05-29T03:21:04.810031Z",
     "shell.execute_reply.started": "2022-05-29T03:21:04.798767Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A    B    C    D\n",
       "a  0.0  1.0  NaN  NaN\n",
       "b  2.0  3.0  NaN  NaN\n",
       "c  4.0  5.0  NaN  NaN\n",
       "a  NaN  NaN  0.0  1.0\n",
       "c  NaN  NaN  2.0  3.0"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# concat数据连接，默认-外连接，默认-axis=0\n",
    "pd.concat([data6,data7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "33a69585-1307-46ca-a5fd-4cb861d80c46",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T03:22:44.197375Z",
     "iopub.status.busy": "2022-05-29T03:22:44.196757Z",
     "iopub.status.idle": "2022-05-29T03:22:44.207748Z",
     "shell.execute_reply": "2022-05-29T03:22:44.206454Z",
     "shell.execute_reply.started": "2022-05-29T03:22:44.197348Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B    C    D\n",
       "a  0  1  0.0  1.0\n",
       "b  2  3  NaN  NaN\n",
       "c  4  5  2.0  3.0"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# concat数据连接，默认-外连接，设定-axis=1\n",
    "pd.concat([data6,data7], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "480e00af-21e8-4583-801d-340328459b1e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-29T03:24:55.022332Z",
     "iopub.status.busy": "2022-05-29T03:24:55.021867Z",
     "iopub.status.idle": "2022-05-29T03:24:55.031408Z",
     "shell.execute_reply": "2022-05-29T03:24:55.030698Z",
     "shell.execute_reply.started": "2022-05-29T03:24:55.022307Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B  C  D\n",
       "a  0  1  0  1\n",
       "c  4  5  2  3"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# concat数据连接，设定-内连接，设定-axis=1\n",
    "pd.concat([data6,data7], join=\"inner\", axis=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
